package com.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.dto.DishDto;
import com.reggie.pojo.Dish;
import com.reggie.pojo.DishFlavor;
import com.reggie.service.impl.CategoryServiceImpl;
import com.reggie.service.impl.DishFlavorServiceImpl;
import com.reggie.service.impl.DishServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController()
@RequestMapping("/dish")
@Slf4j

public class DishController {
    @Autowired
    private DishServiceImpl dishService;
    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private DishFlavorServiceImpl dishFlavorService;


    @GetMapping("/page")
    public R<Page<DishDto>> selectByPage(Integer page, Integer pageSize, String name) {
        /*System.out.println(page + ":" + pageSize);
        Page<Dish> page_now = dishService.selectByPage(page, pageSize, name);
        System.out.println(page_now.getRecords());
        return R.success(page_now);*/
        /*在使用分页查询时直接使用Dish在网页显示中会产生缺点，因此这里在page中封装的数据应该是Dish的数据转换类
        当这一步骤执行完毕后，page_now中已经存储了Dish的值
        需要将Page中的值存入到新的封装类中*/
        Page<Dish> page_now = dishService.selectByPage(page, pageSize, name);
        //现在需要将已经查询到数据的page_now中的数据copy到新的转换类中
        Page<DishDto> page_dishDto = new Page<>();
        //在copy时不copy里面的具体的值，因为里面的值我们需要进一步加工将里面的类别名称赋值
        BeanUtils.copyProperties(page_now, page_dishDto, "records");
        //使用lambda表达式将page_now中的数据进行处理将处理后的结果赋值到page_dishDto中
        List<Dish> records = page_now.getRecords();
        List<DishDto> dishDtos = records.stream().map((item) -> {
            //创建一个DishDto对象
            DishDto dishDto = new DishDto();
            String categoryName = categoryService.selectCategoryNameById(item.getCategoryId());
            dishDto.setCategoryName(categoryName);
            //给种类名称赋值完成后需要将原本的page_now 中的数据也赋值给这个新的dishDto对象中
            BeanUtils.copyProperties(item, dishDto);
            return dishDto;
        }).collect(Collectors.toList());
        page_dishDto.setRecords(dishDtos);
        return R.success(page_dishDto);
    }

    @PostMapping
    //因为使用的是多表操作需要加一个事务,同成功！同失败
    @Transactional
    public R<String> add(@RequestBody DishDto dishDto) {
        log.info("进入了添加服务器");
        //执行添加菜品需要进行多表操作
        dishService.save(dishDto);
        dishDto.getFlavors().stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.save(dishDto);
        return R.success("添加成功！");
    }


    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long... ids) {
        log.info("进入了修改状态服务器");
        System.out.println(status+Arrays.toString(ids));
        //执行修改菜品操作
        boolean flag = dishService.updateStatusById(status, ids);
        if (flag) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    @GetMapping("/{id}")
    public R<DishDto> getDishById(@PathVariable Long id) {
        log.info("进入了修改服务器前的获取前置参数服务器");
        DishDto dishDto = new DishDto();
        Dish dish = dishService.selectById(id);
        BeanUtils.copyProperties(dish, dishDto);
        List<DishFlavor> dishFlavorList = dishFlavorService.selectByDishId(id);
        dishDto.setFlavors(dishFlavorList);
        return R.success(dishDto);
    }

    @PutMapping
    @Transactional
    public R<String> updateDish(@RequestBody DishDto dishDto) {
        log.info("进入了修改服务器");
        //执行修改菜品需要进行多表操作
        //将dishDto中的数据分离出来
        Dish dish = new Dish();
        //给每个口味附上菜品id
        BeanUtils.copyProperties(dishDto, dish);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
        }
        dishFlavorService.updateDishFlavor(flavors, dishDto.getId());
        //修改菜品
        dishService.updateDish(dish);
        return R.success("修改完成！");
    }

    @DeleteMapping
    public R<String> deleteById(Long... ids) {
        log.info("进入了删除服务器");
        log.info("后端取得id为{}", Arrays.toString(ids));
        dishService.deleteByIds(ids);
        dishFlavorService.deleteByDishIds(ids);
        return R.success("删除成功！");
    }


    @GetMapping("/list")
    public R<List<DishDto>> selectDishByCategoryId(Long categoryId) {
        log.info("进入查看具体看某个菜品种类有那些菜品的服务器");
        log.info("现在要获取的菜品种类是{}", categoryId);
        List<DishDto> dishDtos = new ArrayList<>();
        List<Dish> dishes = dishService.selectDishByCategoryId(categoryId);
        for (Dish dish : dishes) {
            if(dish.getStatus() == 1) {
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(dish, dishDto);
                List<DishFlavor> dishFlavors = dishFlavorService.selectByDishId(dish.getId());
                dishDto.setFlavors(dishFlavors);
                dishDtos.add(dishDto);
            }
        }
        System.out.println(dishDtos);
        return R.success(dishDtos);
    }
}
